import {ElementRef, Input, Renderer2} from "@angular/core";

export class Seed {
  private _elementRef: ElementRef;
  private _renderer: Renderer2;
  private _color: string;
  private _componentName: string;

  @Input()
  set color(val: string) {
    this._setColor(val);
  }

  get color(): string {
    return this._color;
  }

  constructor(public elementRef: ElementRef,
              public renderer: Renderer2,
              public componentName?: string) {
    this._elementRef = elementRef;
    this._renderer = renderer;
    this._componentName = componentName;
    if (componentName) {
      this._setComponentName();
    }
  }

  public setElementClass(className: string): void {
    this._renderer.addClass(this._elementRef.nativeElement, className);
  }

  public setElementAttribute(attributeName: string, attributeValue: any): void {
    this._renderer.setAttribute(this._elementRef.nativeElement, attributeName, attributeValue);
  }

  public setElementStyle(style: string, value: string): void {
    this._renderer.setStyle(this._elementRef.nativeElement, style, value);
  }

  private _setColor(newColor: string, componentName?: string) {
    if (componentName) {
      this._componentName = componentName;
    }

    if (this._color) {
      this.setElementClass(`${this._componentName}-${this._color}`);
    }

    if (newColor) {
      this._color = newColor;
      this.setElementClass(`${this._componentName}-${this._color}`);
    }
  }

  private _setComponentName() {
    this.setElementClass(this._componentName);
  }

  public getElementRef(): ElementRef {
    return this._elementRef;
  }

  public getElementElement(): any {
    return this._elementRef.nativeElement;
  }

}
